import org.hibernate.FetchMode as FM;
import grails.converters.JSON;
class MsiInfooldController {
    
    def index = { redirect(action:list,params:params) }

    // the delete, save and update actions only accept POST requests
    static allowedMethods = [delete:'POST', save:'POST', update:'POST']

    def list = {
        params.max = Math.min( params.max ? params.max.toInteger() : 10,  100)
        [ msiInfoInstanceList: MsiInfo.list( params ), msiInfoInstanceTotal: MsiInfo.count() ]
    }

    def show = {
        def msiInfoInstance = MsiInfo.get( params.id )

        if(!msiInfoInstance) {
            flash.message = "MsiInfo not found with id ${params.id}"
            redirect(action:list)
        }
        else { return [ msiInfoInstance : msiInfoInstance ] }
    }

    def delete = {
        def msiInfoInstance = MsiInfo.get( params.id )
        if(msiInfoInstance) {
            msiInfoInstance.delete()
            flash.message = "MsiInfo ${params.id} deleted"
            redirect(action:list)
        }
        else {
            flash.message = "MsiInfo not found with id ${params.id}"
            redirect(action:list)
        }
    }

    def edit = {
        def msiInfoInstance = MsiInfo.get( params.id )

        if(!msiInfoInstance) {
            flash.message = "MsiInfo not found with id ${params.id}"
            redirect(action:list)
        }
        else {
            return [ msiInfoInstance : msiInfoInstance ]
        }
    }

    def update = {
        def msiInfoInstance = MsiInfo.get( params.id )
        if(msiInfoInstance) {
            if(params.version) {
                def version = params.version.toLong()
                if(msiInfoInstance.version > version) {
                    
                    msiInfoInstance.errors.rejectValue("version", "msiInfo.optimistic.locking.failure", "Another user has updated this MsiInfo while you were editing.")
                    render(view:'edit',model:[msiInfoInstance:msiInfoInstance])
                    return
                }
            }
            msiInfoInstance.properties = params
            if(!msiInfoInstance.hasErrors() && msiInfoInstance.save()) {
                flash.message = "MsiInfo ${params.id} updated"
                redirect(action:show,id:msiInfoInstance.id)
            }
            else {
                render(view:'edit',model:[msiInfoInstance:msiInfoInstance])
            }
        }
        else {
            flash.message = "MsiInfo not found with id ${params.id}"
            redirect(action:edit,id:params.id)
        }
    }

    def create = {
        def msiInfoInstance = new MsiInfo()
        msiInfoInstance.properties = params
        return ['msiInfoInstance':msiInfoInstance]
    }

    def save = {
        def msiInfoInstance = new MsiInfo(params)
        if(!msiInfoInstance.hasErrors() && msiInfoInstance.save()) {
            flash.message = "MsiInfo ${msiInfoInstance.id} created"
            redirect(action:show,id:msiInfoInstance.id)
        }
        else {
            render(view:'create',model:[msiInfoInstance:msiInfoInstance])
        }
    }
    def search={
        SearchMsiInfoCommand  searchCmd ->
        def _max = params.limit?params.limit:10
        def _offset = params.start?params.start:0
        def criteria = MsiInfo.createCriteria() //创建了一个 Hibernate 的 Criteria 查询
        def c = MsiInfo.createCriteria()
        def branchCount = c.get {
            projections {
                count "id"
            }
            and {
                //if(searchCmd.domain)
                //like ('domain',"%${searchCmd.domain}%")
                //if(searchCmd.callcenterDN)
                //like ('callcenterDN',"%${searchCmd.callcenterDN}%")
                if(searchCmd.companySN)
                {
                    companySN{
                        like ('companySN',"%${searchCmd.companySN}%" )
                    }
                    fetchMode('companySN', FM.EAGER)
                }
                if(searchCmd.domain)
                {
                    companySN{
                        like ('domain',"%${searchCmd.domain}%" )
                    }
                    fetchMode('companySN', FM.EAGER)
                }
                if(searchCmd.callcenterDN)
                {
                    companySN{
                        like ('callcenterDN',"%${searchCmd.callcenterDN}%" )
                    }
                    fetchMode('companySN', FM.EAGER)
                }
                if(searchCmd.companyName)
                {
                    companySN{
                        like ('companyName',"%${searchCmd.companyName}%" )
                    }
                    fetchMode('companySN', FM.EAGER)
                }
                //if(searchCmd.direction)
                //eq ('direction',Integer.valueOf("${searchCmd.direction}"))
            }
        }
        def resultList = criteria{
            and {
                //if(searchCmd.domain)
                //like ('domain',"%${searchCmd.domain}%")
                //if(searchCmd.callcenterDN)
                //like ('callcenterDN',"%${searchCmd.callcenterDN}%")
                //if(searchCmd.companyName)
                //like ('companyName',"%${searchCmd.companyName}%")
                if(searchCmd.companySN)
                {
                    companySN{
                        like ('companySN',"%${searchCmd.companySN}%" )
                    }
                    fetchMode('companySN', FM.EAGER)
                }
                if(searchCmd.domain)
                {
                    companySN{
                        like ('domain',"%${searchCmd.domain}%" )
                    }
                    fetchMode('companySN', FM.EAGER)
                }
                if(searchCmd.callcenterDN)
                {
                    companySN{
                        like ('callcenterDN',"%${searchCmd.callcenterDN}%" )
                    }
                    fetchMode('companySN', FM.EAGER)
                }
                if(searchCmd.companyName)
                {
                    companySN{
                        like ('companyName',"%${searchCmd.companyName}%" )
                    }
                    fetchMode('companySN', FM.EAGER)
                }

                //如果 teamName 不为空，则与 team 表进行连接，并加入查询条件
                //if(searchCmd.teamName) {
                //team{
                //like ('teamName',"%${searchCmd.teamName}%" )
                //}
                //fetchMode('team', FetchMode.EAGER)
                //}
            }
            maxResults(Integer.valueOf(_max))
            order("id", "asc") //加入了排序的条件，以 id 字段进行升序查询
            firstResult(Integer.valueOf(_offset))
        }


        //flash.message=resultListsum;
        //render resultList as JSON
        render(contentType:"application/json"){
            totalCount(branchCount)
            msis{
                for(a in resultList){
                    msi(id:a.id,
                        agentid:a.agentID,
                        agentdn:a.agentDN,
                        company:a.companySN,
                        companySN:a.companySN.companySN,
                        callcenterDN:a.companySN.callcenterDN,
                        domain:a.domain
                    )
                }
            }
        }
        //println(searchCmd.companyName);
        //render(view:"list" , model:[msiInfoInstanceList: resultList,msiInfoInstanceTotal: branchCount])
    }
    def getuserinfo={
        def companysn = Companysn.findByUserIDAndDomain(params.parentuserid,params.domain)
        def msiinfo = companysn ? MsiInfo.findByCompanySNAndUserID(companysn,params.userid) : null;
	def jsonObj = [];
	def Success=false;
	def accdnses="";
        println("msiinfo")
	println(msiinfo)
	if(companysn==null||msiinfo==null){
	Success=false;
	jsonObj =[result:[success:Success]]
	}else{
	for(a in msiinfo.accdnses.dns){
	  if(accdnses==""){
	   accdnses=a.value.toString()+msiinfo.companySN.companySN
	  }
	  else{
	    accdnses=accdnses.toString()+","+a.value+msiinfo.companySN.companySN
	  }
	}
	Success=true;
	jsonObj =[result:[success:Success,agentid:msiinfo.agentID,companysn:msiinfo.companySN.companySN]]
	//render msiinfo.agentID.toString()+"|"+msiinfo.companySN.companySN+"|"+accdnses
	}
       render jsonObj as JSON; 
    }
}
